// mypages/myfirstpage/myfirstpage.js
var app=new getApp();
var that=this;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: app.globalData.arrowrightsrc,
    idsrc: '/images/my1@2x.png',
    pointsrc:'/images/my2@2x.png',
    headimagesrc:'/images/head@2x.png', // 头像地址
    phonenum:"", // 电话号码
    name: "", // 名称
    identiDict:{
      "00": '未认证',
      "01": '已身份认证',
      "02": '已视频认证',
      "03": '重新制单',
      "04": '已取消'
    },
    identiStr:'', // 认证的汉字状态
    kKtishi:'',//身份重新认证弹框里面的身份认证状态
   },
  //是否认证
  isdentificationhttppost: function () {
    var that = this;
    let url = app.globalRequestUrl('domainNameB', 'siteG');
    let data = {
      Mkt_ID: app.globalData.Mkt_ID,//市场编号
      WeCht_Idr: app.globalData.WeCht_Idr,//微信标识
    }
    app.appRequest('post', url, data, (res) => {
      console.log(res)
      if (res.Txn_Rslt_Cd == "00") {
        /*返回数据存入公共app.js文件*/
        app.globalData.Mrch_Nm = res.Mrch_Nm;  //商户名称
        app.globalData.Mobile_phone = res.MblPh_No;  //商户手机号
        app.globalData.idnt_Ctfn_St = res.Idnt_Ctfn_St;  //身份证状态
        app.globalData.Pcs_St = res.Pcs_St; //流程状态
        app.globalData.Ol_Sign_Jrnl_No = res.Ol_Sign_Jrnl_No;
        app.globalData.identity = res.Mrch_Tp;//商家类型
        that.setData({
          identiStr: that.data.identiDict[res.Idnt_Ctfn_St]
        })
        // ------------------------------------------------------重新认证弹框显示的认证状态  start
        if (app.globalData.idnt_Ctfn_St=='00'){
          this.setData({
            kKtishi:'未认证'
          })
        } else if (app.globalData.idnt_Ctfn_St == '01') {
          this.setData({
            kKtishi: '已身份认证'
          })
        } else if (app.globalData.idnt_Ctfn_St == '02') {
          this.setData({
            kKtishi: '已视频认证'
          })
        } else if (app.globalData.idnt_Ctfn_St == '03') {
          this.setData({
            kKtishi: '重新制单'
          })
        } else if (app.globalData.idnt_Ctfn_St == '04') {
          this.setData({
            kKtishi: '已取消'
          })
        }
        // ------------------------------------------------------重新认证弹框显示的认证状态  end
      } else if (res.Txn_Rslt_Cd == "01") {
        var obj = JSON.parse(message);
        // console.log('错误代码：' + obj.err_CD + '<br>' + '错误信息' + obj.err_Inf)
        app.showErrormessage('错误代码：' + obj.err_CD + '<br>' + '错误信息' + obj.err_Inf);
      } else if (res.Txn_Rslt_Cd == "02") {
        wx.redirectTo({
          url: '/typechoosepage/typechoosepage',
        })
      }
      that.refreshAll(); // 刷新页面
    }, (err) => {
      console.log('请求错误信息：  ' + err.errMsg);
      app.showErrormessage(err.errMsg);
    });
  },

  //身份认证点击方法
  next111:function(){
    var isidentification = app.globalData.idnt_Ctfn_St; //身份认证状态
    if (isidentification == "00") {
      this.next();
    } else if (isidentification == "01") { // 已经身份认证
      var id = app.globalData.identity;
      if (id == "01" || id == "02") { // 跳转视频
        this.nextView();
      }
    } else {

    }
  },

  //跳转身份认证事件
  next: function () {
    var app = getApp();     // 取得全局App
    var id = app.globalData.identity;
    if (id == '01' || id=='02') {
      wx.navigateTo({
        url: '/homepages/personalpages/identitypage/identitypage'
      })
    } else {
      wx.navigateTo({
        url: '/homepages/businesspages/identitypage/identitypage'
      })
    }
  },
  //视频认证事件
  nextView: function () {
    wx.navigateTo({
      //url:'/homepages/commonpages/protocolpage/protocolpage'
       url: '/homepages/personalpages/facerecognpage/facerecognpage'
      })
  },

  //身份重新认证
  rereginise: function () {
    var that = this;
    var pcs_St = app.globalData.Pcs_St;
    if (pcs_St == "" || pcs_St == "2" || pcs_St == "9") {
      wx.showModal({
        title: '提示',
        content: '您目前的身份认证为：' + that.data.kKtishi +'，您需要进行重新身份认证吗？',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.showLoading();
            that.sendReCertificationRequest(); // 重新认证请求
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
    }
  },

  // 重新认证请求
  sendReCertificationRequest: function () {
    var that = this;
    let url = app.globalRequestUrl('domainNameB', 'siteB');
    let data = {
      Mkt_ID: app.globalData.Mkt_ID,//市场编号
      Mkt_Nm: app.globalData.Mkt_Nm,//市场名称
      WeCht_Idr: app.globalData.WeCht_Idr,//微信标识
      Mrch_Tp: app.globalData.identity,//商家类型
    }
    app.appRequest('post', url, data, (res) => {
      wx.hideLoading();
      var message = JSON.stringify(res);
      console.log("接口数据:" + message);

      if (res.Txn_Rslt_Cd == "00") {
        var phone = res.MblPh_No;  //手机号
        var mrch_Nm = res.Mrch_Nm; //商户名称
        var idntCtfnSt = res.Idnt_Ctfn_St; // 身份认证状态
        var pcsSt = res.Pcs_St;  //流程状态

        app.globalData.idnt_Ctfn_St = idntCtfnSt;
        app.globalData.Mrch_Nm = mrch_Nm;
        app.globalData.Mobile_phone = phone;
        app.globalData.Pcs_St = pcsSt;
    
        that.refreshAll(); // 刷新页面

      } else if (res.Txn_Rslt_Cd == "01") {
        var obj = JSON.parse(message);
       // console.log('错误代码：' + obj.err_CD + '<br>' + '错误信息' + obj.err_Inf)
        app.showErrormessage('错误代码：' + obj.err_CD + '<br>' + '错误信息' + obj.err_Inf);
      } else if (res.Txn_Rslt_Cd == "02"){
        wx.showToast({
          url: "typechoosepage/typechoosepage",
        })
      }

    }, (err) => {
      wx.hideLoading();
      console.log('请求错误信息：  ' + err.errMsg);
      app.showErrormessage(err.errMsg);
    });
  },

  //跳转签约指南
  pointnorth: function () {
    wx.navigateTo({
        url: '/mypages/pointnorthpage/pointnorthpage'
      })
  },
  // 阻止跳转
  redsd:function(){
    return false;
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   this.isdentificationhttppost();
   this.refreshAll();
  },

  refreshAll() {
    var that = this;
    that.setData({
      userInfo: app.globalData.userInfo,
      hasUserInfo: true
    });
    that.refreshUserView();
  },

  refreshUserView() {
    var that = this;
    var showName = "";
    if (app.globalData.Mrch_Nm != null && app.globalData.Mrch_Nm != "") {
      showName = app.globalData.Mrch_Nm;
    } else {
      showName = app.globalData.userInfo.nickName;
    }

    var headsrc = '/images/head@2x.png';
    if (app.globalData.userInfo.avatarUrl != null && app.globalData.userInfo.avatarUrl != "") {
      headsrc = app.globalData.userInfo.avatarUrl;
    }

    var phoneNum = app.globalData.Mobile_phone;
    if (phoneNum == null || phoneNum == '') {
      phoneNum = '暂无';
    } else {
      phoneNum = phoneNum.substring(0, 3) + '****' + phoneNum.substring(7);
    }
    console.log("headinfo: ");
    console.log("name: " + showName);
    console.log("head: " + headsrc);
    console.log("phoneNum: " + phoneNum);

    that.setData({
      name: showName,
      headimagesrc: headsrc,
      phonenum: phoneNum,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})